package com.shuimin.as.service;

import pond.common.JSON;
import pond.common.S;
import pond.web.Session;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by edwin on 2/25/2017.
 */
public class NotificationEvent {
    private final String id = S.uuid.vid();
    private String type;
    private String sessionId;
    private final long   timestamp;
    private Object content;

    private NotificationEvent(String type, String sessionId, Object content){
        timestamp = S.now();
        this.type = type;
        this.sessionId = sessionId;
        this.content = content;
    }

    public static NotificationEvent RAW(Session session, String content){
        return new NotificationEvent("raw", session.id(), content);
    }

    public static NotificationEvent NORMAL(Session session, Map<String, Object> json){
        return new NotificationEvent("normal", session.id(), json);
    }

    public static NotificationEvent PROGRESS(Session session,
                                             String taskId,
                                             String taskName,
                                             double progress,int current,int total)
    {
        Map<String,Object> content = new HashMap<>();
        content.put("progress", progress);
        content.put("taskId", taskId);
        content.put("taskName", taskName);
        content.put("current", current);
        content.put("total", total);
        return new NotificationEvent("progress", session.id(), content);
    }

    public static NotificationEvent PROGRESS(String id,
                                             String taskId,
                                             String taskName,
                                             double progress)
    {
        Map<String,Object> content = new HashMap<>();
        content.put("progress", progress);
        content.put("taskId", taskId);
        content.put("taskName", taskName);
        return new NotificationEvent("progress", id, content);
    }

    public static NotificationEvent LOG(Session session,String taskName, String _content){
        Map<String,Object> content = new HashMap<>();
        content.put("taskName", taskName);
        content.put("content",_content);
        return new NotificationEvent("log", session.id(), content);
    }

    public String toJSON(){
        return JSON.stringify(S._tap(new HashMap<String, Object>(), m -> {
            m.put("id", id);
            m.put("type", type);
            m.put("sessionId", sessionId);
            m.put("timestamp", timestamp);
            m.put("content", content);
        }));
    }
}
